package com.evan.review.lifecycle;

import android.util.Log;

import androidx.lifecycle.ViewModel;

/**
 * 生命周期演示ViewModel
 * 用于在配置变更时保持数据状态
 * ViewModel生命周期与Activity实例无关，只有在Activity真正销毁时才会被清除
 */
public class LifecycleViewModel extends ViewModel {
    
    private static final String TAG = "LifecycleViewModel";
    private String lifecycleLog = "";
    private String userInput = "";
    
    public LifecycleViewModel() {
        Log.d(TAG, "ViewModel创建: " + this.hashCode());
    }
    
    /**
     * 获取生命周期日志记录
     */
    public String getLifecycleLog() {
        return lifecycleLog;
    }
    
    /**
     * 设置生命周期日志记录
     */
    public void setLifecycleLog(String lifecycleLog) {
        this.lifecycleLog = lifecycleLog;
    }
    
    /**
     * 获取用户输入文本
     */
    public String getUserInput() {
        return userInput;
    }
    
    /**
     * 设置用户输入文本
     */
    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }
    
    /**
     * ViewModel被销毁时调用
     * 注意：这个方法只在关联的Activity真正结束时才会调用，而不是在配置变更时
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        Log.d(TAG, "ViewModel销毁: " + this.hashCode());
    }
} 